home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap16 / Pipes / Pipes.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.1 KB  |  115 lines

  1. /*--------------------------------------
  2.    PIPES.C -- Palette Animation Demo
  3.               (c) Charles Petzold, 1998
  4.   --------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define ID_TIMER 1
  9.  
  10. TCHAR szAppName [] = TEXT ("Pipes") ;
  11. TCHAR szTitle   [] = TEXT ("Pipes: Palette Animation Demo") ;
  12.  
  13. static LOGPALETTE * plp ;
  14.  
  15. HPALETTE CreateRoutine (HWND hwnd)
  16. {
  17.      HPALETTE hPalette ;
  18.      int      i ;
  19.  
  20.      plp = malloc (sizeof (LOGPALETTE) + 32 * sizeof (PALETTEENTRY)) ;
  21.      
  22.           // Initialize the fields of the LOGPALETTE structure
  23.      
  24.      plp->palVersion    = 0x300 ;
  25.      plp->palNumEntries = 16 ;
  26.  
  27.      for (i = 0 ; i <= 8 ; i++)
  28.      {
  29.           plp->palPalEntry[i].peRed   = (BYTE) min (255, 0x20 * i) ;
  30.           plp->palPalEntry[i].peGreen = 0 ;
  31.           plp->palPalEntry[i].peBlue  = (BYTE) min (255, 0x20 * i) ;
  32.           plp->palPalEntry[i].peFlags = PC_RESERVED ;
  33.  
  34.           plp->palPalEntry[16 - i] = plp->palPalEntry[i] ;
  35.           plp->palPalEntry[16 + i] = plp->palPalEntry[i] ;
  36.           plp->palPalEntry[32 - i] = plp->palPalEntry[i] ;
  37.      }
  38.  
  39.      hPalette = CreatePalette (plp) ;
  40.      
  41.      SetTimer (hwnd, ID_TIMER, 100, NULL) ;
  42.      return hPalette ;
  43. }
  44.  
  45. void PaintRoutine (HDC hdc, int cxClient, int cyClient)
  46. {
  47.      HBRUSH hBrush ;
  48.      int    i ;
  49.      RECT   rect ;
  50.  
  51.           // Draw window background
  52.  
  53.      SetRect (&rect, 0, 0, cxClient, cyClient) ;
  54.      hBrush = SelectObject (hdc, GetStockObject (WHITE_BRUSH)) ;
  55.      FillRect (hdc, &rect, hBrush) ;
  56.  
  57.           // Draw the interiors of the pipes
  58.  
  59.      for (i = 0 ; i < 128 ; i++)
  60.      {
  61.           hBrush = CreateSolidBrush (PALETTEINDEX (i % 16)) ;
  62.           SelectObject (hdc, hBrush) ;
  63.  
  64.           rect.left   = (127 - i) * cxClient / 128 ;
  65.           rect.right  = (128 - i) * cxClient / 128 ;
  66.           rect.top    = 4 * cyClient / 14 ;
  67.           rect.bottom = 5 * cyClient / 14 ;
  68.  
  69.           FillRect (hdc, &rect, hBrush) ;
  70.  
  71.           rect.left   =  i      * cxClient / 128 ;
  72.           rect.right  = (i + 1) * cxClient / 128 ;
  73.           rect.top    =  9 * cyClient / 14 ;
  74.           rect.bottom = 10 * cyClient / 14 ;
  75.  
  76.           FillRect (hdc, &rect, hBrush) ;
  77.  
  78.           DeleteObject (SelectObject (hdc, GetStockObject (WHITE_BRUSH))) ;
  79.      }
  80.  
  81.           // Draw the edges of the pipes
  82.  
  83.      MoveToEx (hdc, 0,         4 * cyClient / 14, NULL) ;
  84.      LineTo   (hdc, cxClient,  4 * cyClient / 14) ;
  85.  
  86.      MoveToEx (hdc, 0,         5 * cyClient / 14, NULL) ;
  87.      LineTo   (hdc, cxClient,  5 * cyClient / 14) ;
  88.  
  89.      MoveToEx (hdc, 0,         9 * cyClient / 14, NULL) ;
  90.      LineTo   (hdc, cxClient,  9 * cyClient / 14) ;
  91.  
  92.      MoveToEx (hdc, 0,        10 * cyClient / 14, NULL) ;
  93.      LineTo   (hdc, cxClient, 10 * cyClient / 14) ;
  94.      return ;
  95. }
  96.  
  97. void TimerRoutine (HDC hdc, HPALETTE hPalette)
  98. {
  99.      static int iIndex ;
  100.  
  101.      AnimatePalette (hPalette, 0, 16, plp->palPalEntry + iIndex) ;
  102.  
  103.      iIndex = (iIndex + 1) % 16 ;
  104.  
  105.      return ;
  106. }
  107.  
  108. void DestroyRoutine (HWND hwnd, HPALETTE hPalette)
  109. {
  110.      KillTimer (hwnd, ID_TIMER) ;
  111.      DeleteObject (hPalette) ;
  112.      free (plp) ;
  113.      return ;
  114. }
  115.